package com.test.cordova;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.cordova.glide.GlideUtils;
import com.test.cordova.provider.TestUtil;
import com.test.cordova.util.IntentUtils;
import com.test.cordova.util.Logger;
import com.test.cordova.util.ScreenUtil;
import com.test.cordova.view.CustomCircleImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class StartActivity extends Activity {
    @BindView(R.id.start_vp)
    public ViewPager vp;

    @BindView(R.id.start_jump_tip)
    public TextView start_jump_tip;

    @BindView(R.id.progressImage)
    public CustomCircleImage progressImage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //点击桌面图标防止重复启动 （App已运行后台）
        if (!isTaskRoot()) {
            finish();
            return;
        }
        ScreenUtil.setCustomDensity(this, getApplication());
        setContentView(R.layout.start_layout);

        Logger.setDebugMode(true);
        ButterKnife.bind(this);
        initData();
        start_jump_tip.setText(String.format(getString(R.string.start_jump_tip), currentTime));
        StartViewPagerAdapter adapter = new StartViewPagerAdapter();
        vp.setAdapter(adapter);

        //模拟下载进度条
        download.start();
        //测试ContentProvider
        TestUtil.getInstance().test(this);


    }

    private Thread download = new Thread(new Runnable() {
        @Override
        public void run() {

            int progress = 0;
            while (progress < 100) {
                progress += 10;
                int finalProgress = progress;
                Logger.e("progress", "" + Thread.currentThread().getName() + " finalProgress: " + finalProgress);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressImage.setProgress(finalProgress);
                        Logger.e("progress", "" + Thread.currentThread().getName() + " finalProgress: " + finalProgress);
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    private void initData() {
        data.add("http://pic14.nipic.com/20110605/1369025_165540642000_2.jpg");
        data.add("http://pic14.nipic.com/20110605/1369025_165540642000_2.jpg");
        delayJump();
    }

    @OnClick(value = R.id.start_jump_tip)
    public void jump(View v) {

        v.animate().setDuration(3).scaleX(1.50f).scaleY(1.50f).withEndAction(new Runnable() {
            @Override
            public void run() {
                startJump();
            }
        }).start();

    }

    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {
            currentTime--;
            start_jump_tip.setText(String.format(getString(R.string.start_jump_tip), currentTime));
            if (currentTime == 0) {
                startJump();
            } else {
                mhandle.postDelayed(this, 1000);
            }
        }
    };

    private void startJump() {
        mhandle.removeCallbacks(timeRunable);
        mhandle = null;
        IntentUtils.startActivity(this,MainActivity.class,null);
        finish();
        //overridePendingTransition(0,0);
    }

    //计时器
    private Handler mhandle = new Handler();
    private int currentTime = 10;

    private void delayJump() {
        mhandle.post(timeRunable);
    }

    private List<String> data = new ArrayList<>();

    class StartViewPagerAdapter extends PagerAdapter {

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView iv = new ImageView(StartActivity.this);
            if (getCount() == 1) {
                iv.setImageResource(R.mipmap.icon);
                iv.setScaleType(ImageView.ScaleType.CENTER);
                container.addView(iv);
            } else {
                GlideUtils.loadImageView(StartActivity.this, data.get(position), iv);
                container.addView(iv);
            }

            return iv;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeViewAt(position);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

    }


}
